<html>
<head><meta charset="utf-8"><title>Can we replace the stack in &quot;stacked borrows&quot; by Drop? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html">Can we replace the stack in &quot;stacked borrows&quot; by Drop?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="165536441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165536441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165536441">(May 13 2019 at 15:01)</a>:</h4>
<p>Moving a conversation with <span class="user-mention" data-user-id="120791">@RalfJ</span> into its own thread...</p>



<a name="165536496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165536496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165536496">(May 13 2019 at 15:01)</a>:</h4>
<p>The conversation was in <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Slides.20for.20talk.20on.20unsafe.20rust" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Slides.20for.20talk.20on.20unsafe.20rust">https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Slides.20for.20talk.20on.20unsafe.20rust</a></p>



<a name="165536608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165536608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165536608">(May 13 2019 at 15:02)</a>:</h4>
<p>and was about whether we could use the drop mechanism that already exists in Rust to replace the per-address stacks that are in stacked borrows</p>



<a name="165536697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165536697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165536697">(May 13 2019 at 15:03)</a>:</h4>
<p>A draft implementation (missing a lot of Rust) is at <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=90773fba2b71073841015190bc8c1fdb" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=90773fba2b71073841015190bc8c1fdb">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=90773fba2b71073841015190bc8c1fdb</a></p>



<a name="165537011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165537011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165537011">(May 13 2019 at 15:06)</a>:</h4>
<p>in particular, as <span class="user-mention" data-user-id="120791">@RalfJ</span> pointed out, it's missing interior mutability, and in particular that interior mutability allows the same memory address to have multiple simultanous access modes, e.g. a <code>&amp;RefCell&lt;u32&gt;</code> may have the same address as a <code>&amp;mut u32</code>.</p>



<a name="165537261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165537261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165537261">(May 13 2019 at 15:09)</a>:</h4>
<p>There's also the point of ergonomics and integration with Miri, since this introduces "shadow drop code" to types that don't currently have Drop, e.g. when a borrow or reborrow finishes, the metadata is updated, and this is something that Miri doesn't track.</p>



<a name="165537283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165537283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165537283">(May 13 2019 at 15:09)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> does that seem like a fair summary of where we are?</p>



<a name="165557783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165557783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165557783">(May 13 2019 at 18:51)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="165557899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165557899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165557899">(May 13 2019 at 18:52)</a>:</h4>
<p>there's also the question about <code>&amp;T</code> which is <code>Copy</code> and hence cannot be <code>Drop</code>. you proposed some non-<code>Copy</code> variant of shared references but I am unconvinced of that^^</p>



<a name="165567222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165567222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165567222">(May 13 2019 at 20:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> yes, we'd need some non-copy marker for updating the metadata of memory addresses when it is dropped.</p>



<a name="165567307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165567307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165567307">(May 13 2019 at 20:44)</a>:</h4>
<p>I'm looking to see how difficult it'll be to get your example with <code>RefCell</code> into the code I wrote yesterday. We'll see.</p>



<a name="165569266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165569266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165569266">(May 13 2019 at 21:07)</a>:</h4>
<p>Clearly this code is all wrong: <code>Colour</code>... <span aria-label="stuck out tongue wink" class="emoji emoji-1f61c" role="img" title="stuck out tongue wink">:stuck_out_tongue_wink:</span></p>



<a name="165588832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165588832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165588832">(May 14 2019 at 03:09)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  OK, I got your example with aliased <code>RefCell</code> to go through: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4007d27c87e5b03dd1c3ff6a4fd501a6" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4007d27c87e5b03dd1c3ff6a4fd501a6">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4007d27c87e5b03dd1c3ff6a4fd501a6</a></p>



<a name="165588900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165588900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165588900">(May 14 2019 at 03:10)</a>:</h4>
<p>The trick here is that a <code>RecCell&lt;T&gt;</code> has at least one more byte of memory than a <code>T</code>,  and we can use that byte to keep the metadata for the <code>RefCell&lt;T&gt;</code> separate from the metadata for the <code>T</code>.</p>



<a name="165588924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165588924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165588924">(May 14 2019 at 03:11)</a>:</h4>
<p>This trick wouldn't work for <code>UnsafeCell&lt;T&gt;</code>.</p>



<a name="165589092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165589092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165589092">(May 14 2019 at 03:12)</a>:</h4>
<p>Small edit to remove some dead code: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8597244d346a2e82a45a6a731f74136b" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8597244d346a2e82a45a6a731f74136b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8597244d346a2e82a45a6a731f74136b</a></p>



<a name="165598931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165598931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165598931">(May 14 2019 at 07:12)</a>:</h4>
<blockquote>
<p>The trick here is that a <code>RecCell&lt;T&gt;</code> has at least one more byte of memory than a <code>T</code>,  and we can use that byte to keep the metadata for the <code>RefCell&lt;T&gt;</code> separate from the metadata for the <code>T</code>.</p>
</blockquote>
<p>so, the model needs to specifically know <code>RefCell</code> and color it differently?</p>



<a name="165598986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165598986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165598986">(May 14 2019 at 07:13)</a>:</h4>
<p>Stacked Borrows does need to know (for shared references only) whether something is inside an <code>UnsafeCell</code> or not, that is inevitable -- but the model cannot have any idea about the fact that <code>RefCell</code>'s borrow counter has anything to do with that. in fact, that one is in an <code>UnsafeCell</code> as well, so it should get the exact same treatment.</p>



<a name="165599070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165599070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165599070">(May 14 2019 at 07:15)</a>:</h4>
<p>the goal of this model is to enable the compiler to do better program analysis (alias analysis specifically). I dont think that can work if the model is customizable with user code -- then the compiler has to understand that user code to know what information it can even deduce.</p>



<a name="165625562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165625562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165625562">(May 14 2019 at 14:12)</a>:</h4>
<p>OK, so to treat this properly, I'd need to implement a <code>MyRefCell&lt;T&gt;</code> in order to get at the private fields of the ref cell, one of which would be an <code>UnsafeCell&lt;T&gt;</code>.</p>



<a name="165625764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165625764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165625764">(May 14 2019 at 14:15)</a>:</h4>
<p>My guess is that this is doable, using the same sort of model as before, that <code>cell.get()</code> will put down a marker whose drop code will update the metadata.</p>



<a name="165626339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165626339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165626339">(May 14 2019 at 14:22)</a>:</h4>
<p>It might not even need that, the markers for casting <code>*mut T</code> to <code>&amp;T</code> or <code>&amp;mut T</code> might be enough, we'll see.</p>



<a name="165626361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165626361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165626361">(May 14 2019 at 14:22)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I'll have a shot at this tonight.</p>



<a name="165626430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165626430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165626430">(May 14 2019 at 14:23)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> BTW, thanks for walking this through with me, it's really helping me to understand stacked borrows!</p>



<a name="165627804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165627804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165627804">(May 14 2019 at 14:39)</a>:</h4>
<p>I'm curious where it'll lead. :D And maybe there is some kind of equivalence or so, maybe some way to justify some of my arbitrary decisions.</p>
<p>But I also have to say that I personally don't like "ghost code"-style stuff. I've held this opinion mostly as a matter of principle for some time, and then recently in my research we did "ghost code"-style stuff for the first time... and it's awful, I see myself 100% confirmed. So I think if a more complex "instrumented state" like a stack lets us avoid having to insert code in complex ways, I think that alone would be totally worth it.</p>



<a name="165632135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632135">(May 14 2019 at 15:30)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> yeah, there may be a trade-off where the metadata can be made simpler but at the cost of introducing ghost code.</p>



<a name="165632280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632280">(May 14 2019 at 15:32)</a>:</h4>
<p>basically, you are encoding the stack implicitly in the well-nested control flow / RAII you are introducing</p>



<a name="165632604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632604">(May 14 2019 at 15:36)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Miri does have StorageLive / Storage</p>



<a name="165632612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632612">(May 14 2019 at 15:36)</a>:</h4>
<p>Dead</p>



<a name="165632617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632617">(May 14 2019 at 15:36)</a>:</h4>
<p>it does</p>



<a name="165632626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632626">(May 14 2019 at 15:36)</a>:</h4>
<p>We could hang metadata updates on those.</p>



<a name="165632632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632632">(May 14 2019 at 15:36)</a>:</h4>
<p>that doesnt really help for heap-allocated stuff though</p>



<a name="165632653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632653">(May 14 2019 at 15:37)</a>:</h4>
<p>also, not all variables have these annotations</p>



<a name="165632795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632795">(May 14 2019 at 15:38)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> yeah, there's probably cases of, e.g. &amp;mut to &amp;, conversions that don't end up being tracked by Miri :/</p>



<a name="165632825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632825">(May 14 2019 at 15:39)</a>:</h4>
<p>and even then, I feel it is better to tell people "your reference is considered live until it is used the last time" rather than "your reference is considered live until a point which the compiler magically determines and which you cannot see". I am also worried about how this would permanently fix where we put these "end of life"-annotations; moving them either way could break code.</p>



<a name="165632982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165632982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165632982">(May 14 2019 at 15:40)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> stacked borrows has that a reference is considered live until a use of an earlier reference?</p>



<a name="165633131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633131">(May 14 2019 at 15:42)</a>:</h4>
<p>e.g. in <code>let x = &amp;mut 37; { let y = &amp;*x; ... } *x += 1;</code> it's the use of <code>x</code> that ends the lifetime of <code>y</code>?</p>



<a name="165633146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633146">(May 14 2019 at 15:42)</a>:</h4>
<p>I'd say it is considered "live" until it is used the last time</p>



<a name="165633160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633160">(May 14 2019 at 15:42)</a>:</h4>
<p>and then using an earlier reference while the child is still live is UB</p>



<a name="165633166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633166">(May 14 2019 at 15:42)</a>:</h4>
<p>but there are many ways to say the same thing here</p>



<a name="165633209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633209">(May 14 2019 at 15:43)</a>:</h4>
<p>and to be fair, my characterization misplaces where the UB happens -- it happens when the child gets used again, as only then we really know that it was live all along</p>



<a name="165633279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633279">(May 14 2019 at 15:44)</a>:</h4>
<p>so in this view, the stack tracks "pointers that are allowed to still be live"</p>



<a name="165633301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633301">(May 14 2019 at 15:44)</a>:</h4>
<p>yes, the question is where that stack lives,</p>



<a name="165633328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633328">(May 14 2019 at 15:44)</a>:</h4>
<p>in stacked borrows each memory address has its own stack,</p>



<a name="165633362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633362">(May 14 2019 at 15:45)</a>:</h4>
<p>I prefer this view when explaining conflicts to people: "see, you have this pointer here that you used again there, so clearly it was live all the time, but then there is this other conflicting access that happened while the pointer was live"</p>



<a name="165633373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633373">(May 14 2019 at 15:45)</a>:</h4>
<p>it would be nice if we could use the call stack for this.</p>



<a name="165633410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633410">(May 14 2019 at 15:45)</a>:</h4>
<blockquote>
<p>it would be nice if we could use the call stack for this.</p>
</blockquote>
<p>fair</p>



<a name="165633477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633477">(May 14 2019 at 15:46)</a>:</h4>
<p>but given that it's not even really a stack any more, I doubt that</p>



<a name="165633492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633492">(May 14 2019 at 15:46)</a>:</h4>
<p>I am inserting and removing "in the middle" on a few occasions and found no way around that</p>



<a name="165633542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633542">(May 14 2019 at 15:47)</a>:</h4>
<p>Is it interior mutability that causes those updates "in the middle"?</p>



<a name="165633676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633676">(May 14 2019 at 15:49)</a>:</h4>
<p>the example I posted with aliasing mutable/shared refs is one case</p>



<a name="165633684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633684">(May 14 2019 at 15:49)</a>:</h4>
<p>two-phase borrows is another</p>



<a name="165633693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165633693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165633693">(May 14 2019 at 15:49)</a>:</h4>
<p>there are probably more but I am not sure</p>



<a name="165634020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165634020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165634020">(May 14 2019 at 15:52)</a>:</h4>
<p>OK, I'll have a look at 2-phase borrows.</p>



<a name="165634048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165634048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165634048">(May 14 2019 at 15:52)</a>:</h4>
<p>I'll get a chance to think about this tonight.</p>



<a name="165634055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165634055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165634055">(May 14 2019 at 15:53)</a>:</h4>
<p>Hopefully.</p>



<a name="165635422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165635422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165635422">(May 14 2019 at 16:06)</a>:</h4>
<p>what would help me in understanding this is: (a) some kind of trace of what is going on in that RefCell thing -- like, annotating the code with how the colour changes, or so. I find that had to extract from the implementation. (b) some kind of argument along the lines of <a href="https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html#5-key-properties" target="_blank" title="https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html#5-key-properties">https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html#5-key-properties</a>: why do the checks that you are inserting <em>guarantee</em> that some unknown code does not mess with our memory?</p>



<a name="165640218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165640218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165640218">(May 14 2019 at 17:00)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> a trace should be pretty easy. A proof of correctness might be a bit trickier :)</p>



<a name="165640909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165640909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165640909">(May 14 2019 at 17:08)</a>:</h4>
<p>well, I was more thinking of a reasonable argument than a full formal proof. ;)</p>



<a name="165640936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165640936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165640936">(May 14 2019 at 17:09)</a>:</h4>
<p>I wouldn't call what I got in that blog post a proof, that's a proof sketch at best.^^</p>



<a name="165641369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165641369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165641369">(May 14 2019 at 17:14)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> fair enough.</p>



<a name="165739525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165739525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165739525">(May 15 2019 at 18:20)</a>:</h4>
<p>I reliazed that if we think of <code>&amp;T</code> as being a refcounting type where the refcount is metadata then we get a very simple model (the metadata for each byte is the same as for <code>Rc&lt;u8&gt;</code>). <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0bc4a04c1186ee641450be1b03aaa031" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0bc4a04c1186ee641450be1b03aaa031">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0bc4a04c1186ee641450be1b03aaa031</a></p>



<a name="165739659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165739659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165739659">(May 15 2019 at 18:22)</a>:</h4>
<p>Does suffer from the same problem as before, which is that we have to think of <code>&amp;T</code> as a type which implements <code>Clone</code> and <code>Drop</code>, even though those only update the metadata.</p>



<a name="165739677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165739677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165739677">(May 15 2019 at 18:22)</a>:</h4>
<p>But the model is really really simple.</p>



<a name="165739772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165739772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165739772">(May 15 2019 at 18:23)</a>:</h4>
<p>Oh, and I've not looked at <code>UnsafeCell</code> yet, I don't thiiiiiink there's anything more needed, but devil in the details.</p>



<a name="165739786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165739786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165739786">(May 15 2019 at 18:24)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="165740520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165740520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165740520">(May 15 2019 at 18:31)</a>:</h4>
<p>Hmm, in fact there may be a way to implement all of this in safe rust (except for the casts from <code>*T</code> to <code>*U</code> which are blatantly unsafe).</p>



<a name="165740795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165740795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165740795">(May 15 2019 at 18:34)</a>:</h4>
<p>This reminds me of ECSs, in that it implements refcounting, but keeps all the refcounts together. I wonder if there's an implementation of an arena that already does this?</p>



<a name="165753176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165753176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165753176">(May 15 2019 at 20:46)</a>:</h4>
<blockquote>
<p>I reliazed that if we think of &amp;T as being a refcounting type where the refcount is metadata then we get a very simple model (the metadata for each byte is the same as for Rc&lt;u8&gt;).</p>
</blockquote>
<p>this reminds me of some of the things I did back in 2017 with my first attempt at defining what references may and may not do. I had RwLocks back then. But unfortunately that turned out not enough when you have RefCell in RefCell in RefCell... each of them is basically a new layer of RwLock.</p>



<a name="165754354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165754354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165754354">(May 15 2019 at 20:58)</a>:</h4>
<p>The good news is that each level of <code>RefCell</code> has at least one byte of its own data, which can be coloured independently of the contents.</p>



<a name="165754388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165754388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165754388">(May 15 2019 at 20:59)</a>:</h4>
<p>I should look to see whether I can code up a <code>MyRefCell&lt;T&gt;</code> using <code>UnsafeCell&lt;T&gt;</code> and get it to fly.</p>



<a name="165754533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165754533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165754533">(May 15 2019 at 21:01)</a>:</h4>
<blockquote>
<p>The good news is that each level of <code>RefCell</code> has at least one byte of its own data, which can be coloured independently of the contents.</p>
</blockquote>
<p><code>RefCell</code> is just an example, this can happen without any extra data in unsafe code</p>



<a name="165754541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165754541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165754541">(May 15 2019 at 21:01)</a>:</h4>
<p>the model cannot know where the synchronization is coming from</p>



<a name="165754661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165754661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165754661">(May 15 2019 at 21:03)</a>:</h4>
<p>and in code like this, there is no extra byte:</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">cell</span>::<span class="n">UnsafeCell</span><span class="p">;</span><span class="w"></span>

<span class="c1">// Two-phase borrows of the pointer returned by UnsafeCell::get() should not</span>
<span class="c1">// invalidate aliases.</span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">UnsafeCell</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="c1">// We can have a unique reference</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">uniq_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">uniq_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="c1">// and then use our shared references again</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x2</span><span class="p">.</span><span class="n">get</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165759135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165759135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165759135">(May 15 2019 at 22:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  I coded up your example ^ and got it to run: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8d190468b1d048261cedce9ac9b2f87c" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8d190468b1d048261cedce9ac9b2f87c">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=8d190468b1d048261cedce9ac9b2f87c</a></p>



<a name="165759182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165759182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165759182">(May 15 2019 at 22:05)</a>:</h4>
<p>There is an explicit <code>drop(uniq_ref)</code>, without that it UBs.</p>



<a name="165759254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165759254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165759254">(May 15 2019 at 22:06)</a>:</h4>
<p>Same trade-off, simpler model, but uses <code>clone</code> and <code>drop</code>.</p>



<a name="165786335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786335">(May 16 2019 at 07:26)</a>:</h4>
<p>I am afraid without a tracce I have no idea what is going on :/ it would take forever to dig through that code. But I am convinced that your model inherently does not have enough state to properly model nested <code>UnsafeCell</code></p>



<a name="165786343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786343">(May 16 2019 at 07:26)</a>:</h4>
<p>I can have N shared refs to the outer cell and M to the inner, (and K to the 3rd nested UnsafeCell and so on) and these two "refcounts" must be tracked separately</p>



<a name="165786354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786354">(May 16 2019 at 07:26)</a>:</h4>
<p>I don't see that happen, so something must be missing somewhere.</p>



<a name="165786382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786382">(May 16 2019 at 07:28)</a>:</h4>
<p>and then what about raw pointers, do you plan to have refcounts for those as well?</p>



<a name="165786468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786468">(May 16 2019 at 07:29)</a>:</h4>
<p>Also I see no checks that happen on accesses? That does not seem right, sometimes the only difference between okay and UB is whether some access is a read or a write.</p>



<a name="165786562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165786562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165786562">(May 16 2019 at 07:31)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span>  like this:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">);</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Reading from x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...keeps y valid.</span>
<span class="w">   </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Writing to x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...kills y. This line is UB.</span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165818369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818369">(May 16 2019 at 15:03)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> the example does have a trace showing the colourings, are you looking for more detail?</p>



<a name="165818527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818527">(May 16 2019 at 15:04)</a>:</h4>
<p>the refcounts are on memory addresses, not on values, so raw pointers don't carry refcounts, but the memory pointed to by the raw pointer does.</p>



<a name="165818689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818689">(May 16 2019 at 15:06)</a>:</h4>
<p>one thing that's interesting about the refcount model is that references are just integers, all the metadata is tracked in the memory being referred to.</p>



<a name="165818745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818745">(May 16 2019 at 15:06)</a>:</h4>
<p>Making the drop explicit, your example is:</p>



<a name="165818746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818746">(May 16 2019 at 15:06)</a>:</h4>
<div class="codehilite"><pre><span></span>
</pre></div>



<a name="165818782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818782">(May 16 2019 at 15:07)</a>:</h4>
<p>sigh, <code>Enter</code> == send message, sigh</p>



<a name="165818920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818920">(May 16 2019 at 15:08)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">);</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Reading from x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...keeps y valid.</span>
<span class="w">   </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Writing to x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...kills y. This line is UB.</span>
<span class="w">  </span><span class="n">drop</span><span class="p">(</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// At this point we drop y</span>
<span class="w">  </span><span class="n">drop</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165818993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165818993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165818993">(May 16 2019 at 15:09)</a>:</h4>
<p>whereas the version that doesn't UB is:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">);</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Reading from x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...keeps y valid.</span>
<span class="w">  </span><span class="n">drop</span><span class="p">(</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// At this point we drop y</span>
<span class="w">   </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Writing to x...</span>
<span class="w">  </span><span class="n">drop</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165819067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819067">(May 16 2019 at 15:10)</a>:</h4>
<p>That is, it's when the drop code runs that determines whether the behaviour is UB or not.</p>



<a name="165819115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819115">(May 16 2019 at 15:10)</a>:</h4>
<p>(which brings us to the core trade-off, simpler model vs UB based on invisible shadow code)</p>



<a name="165819606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819606">(May 16 2019 at 15:16)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> the example does have a trace showing the colourings, are you looking for more detail?</p>
</blockquote>
<p>d'oh, I never thought of <em>running</em> it^^</p>



<a name="165819703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819703">(May 16 2019 at 15:17)</a>:</h4>
<p>:)</p>



<a name="165819822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819822">(May 16 2019 at 15:19)</a>:</h4>
<blockquote>
<p>one thing that's interesting about the refcount model is that references are just integers, all the metadata is tracked in the memory being referred to.</p>
</blockquote>
<p>I think that can inherently not protected against "misbehaving" unsafe code though... like, in</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="n">bar</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165819955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165819955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165819955">(May 16 2019 at 15:20)</a>:</h4>
<p>we want to be sure that <code>bar</code> cannot write to this memory -- but if it has a raw pointer to that memory, without having metadata in the pointer, how would the model distinguish that from using <code>x</code> to access the same memory (which is legitimate)?</p>



<a name="165820200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820200">(May 16 2019 at 15:23)</a>:</h4>
<blockquote>
<p>(which brings us to the core trade-off, simpler model vs UB based on invisible shadow code)</p>
</blockquote>
<p>well at this point you also have consider the pass adding the shadow code to be part of the model, in terms of complexity. and it seems that has to be quite complex. like, how would it know when exactly to drop <code>y</code> here, when lifetimes are unreliable?</p>



<a name="165820401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820401">(May 16 2019 at 15:25)</a>:</h4>
<p>so re: the example you posted, why does it not colour the memory unique when I create a mutable reference? seems like <code>uniq_ref</code> is "less unique" than 'cell'?</p>



<a name="165820426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820426">(May 16 2019 at 15:25)</a>:</h4>
<blockquote>
<blockquote>
<p>(which brings us to the core trade-off, simpler model vs UB based on invisible shadow code)</p>
</blockquote>
<p>well at this point you also have consider the pass adding the shadow code to be part of the model, in terms of complexity. and it seems that has to be quite complex. like, how would it know when exactly to drop <code>y</code> here, when lifetimes are unreliable?</p>
</blockquote>
<p>True, though that complexity is a lot like the existing complexity of when does <code>Drop</code> code get inserted.</p>



<a name="165820535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820535">(May 16 2019 at 15:26)</a>:</h4>
<p>TBH I doubt that, drop only has to work with moves, not borrows, and it is specifically not trying to catch misbehaving unsafe code</p>



<a name="165820550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820550">(May 16 2019 at 15:26)</a>:</h4>
<p>like, if you <code>drop(ptr::read(&amp;mut local))</code>, <code>local</code> will get double-dropped</p>



<a name="165820621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820621">(May 16 2019 at 15:28)</a>:</h4>
<p>I said "a lot like" not "exactly the same as" :)</p>



<a name="165820698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820698">(May 16 2019 at 15:28)</a>:</h4>
<p>I know :)</p>



<a name="165820762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820762">(May 16 2019 at 15:29)</a>:</h4>
<p>and btw if you are looking for more testcases, I got a few at <a href="https://github.com/rust-lang/miri/tree/master/tests/run-pass/stacked-borrows" target="_blank" title="https://github.com/rust-lang/miri/tree/master/tests/run-pass/stacked-borrows">https://github.com/rust-lang/miri/tree/master/tests/run-pass/stacked-borrows</a> and <a href="https://github.com/rust-lang/miri/tree/master/tests/compile-fail/stacked_borrows" target="_blank" title="https://github.com/rust-lang/miri/tree/master/tests/compile-fail/stacked_borrows">https://github.com/rust-lang/miri/tree/master/tests/compile-fail/stacked_borrows</a></p>



<a name="165820880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820880">(May 16 2019 at 15:30)</a>:</h4>
<p>Ta.</p>



<a name="165820897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820897">(May 16 2019 at 15:30)</a>:</h4>
<p>"Ta."?</p>



<a name="165820911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820911">(May 16 2019 at 15:31)</a>:</h4>
<p>Thanks for the testcases.</p>



<a name="165820925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820925">(May 16 2019 at 15:31)</a>:</h4>
<p>TIL: <a href="https://www.dict.cc/?s=Ta" target="_blank" title="https://www.dict.cc/?s=Ta">https://www.dict.cc/?s=Ta</a></p>



<a name="165820935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820935">(May 16 2019 at 15:31)</a>:</h4>
<p>never saw that before^^ but it says "Br/Aus", so I guess that explains why ;)</p>



<a name="165820946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165820946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165820946">(May 16 2019 at 15:31)</a>:</h4>
<p>I am so unaware of what words are colloquial.</p>



<a name="165821271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165821271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165821271">(May 16 2019 at 15:35)</a>:</h4>
<p>I'll have another think about the refcounted memory model, hopefully tonight.</p>



<a name="165821341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165821341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165821341">(May 16 2019 at 15:36)</a>:</h4>
<p>I have GL errors from VR browsers to sort out atm :)</p>



<a name="165909151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909151">(May 17 2019 at 15:22)</a>:</h4>
<p>A simplified version, which keeps track of how many unique refs and shared refs exist: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7bb59a62c3713efc40b6b9d6e90e6b20" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7bb59a62c3713efc40b6b9d6e90e6b20">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7bb59a62c3713efc40b6b9d6e90e6b20</a></p>



<a name="165909212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909212">(May 17 2019 at 15:23)</a>:</h4>
<p>There's a count of unique references due to reborrowing, which increments the count of unique refs.</p>



<a name="165909415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909415">(May 17 2019 at 15:25)</a>:</h4>
<p>If they're both non-zero, that's because a <code>&amp;mut T</code> got reborrowed as a <code>&amp;T</code> (there's a <code>Frozen</code> marker put on the stack to check that there's no live <code>&amp;T</code>s when the <code>&amp;mut T</code> becomes live again).</p>



<a name="165909597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909597">(May 17 2019 at 15:27)</a>:</h4>
<p>Same objections as before... this needs to keep track of when references are cloned, reborrowed and dropped, which Miri may not be very happy about :/</p>



<a name="165909770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909770">(May 17 2019 at 15:29)</a>:</h4>
<p>But the model is very simple, and references are just integers, all the metadata is attached to memory addresses.</p>



<a name="165909877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165909877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165909877">(May 17 2019 at 15:30)</a>:</h4>
<p>Next stage is probably trying more examples.</p>



<a name="165910013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165910013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165910013">(May 17 2019 at 15:31)</a>:</h4>
<p>I'm going to be in SF next week for S&amp;P (hearing about Spectre and RIDL and talking about <a href="https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf" target="_blank" title="https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf">https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf</a>) so the next step won't be for a bit.</p>



<a name="165910024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165910024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165910024">(May 17 2019 at 15:31)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="165921029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921029">(May 17 2019 at 17:47)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> thanks!</p>



<a name="165921141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921141">(May 17 2019 at 17:48)</a>:</h4>
<p>but this means that with nested <code>UnsafeCell</code> you do not distinguish shared references from the different levels. there's a lot of complexity there that you are just not implementing -- or maybe moving into the pass that inserts the drops? not sure.</p>



<a name="165921203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921203">(May 17 2019 at 17:49)</a>:</h4>
<p>also what if the position of the drop cannot be predicted statically? let me revive one of my earlier examples where I said you have to distinguish pointers</p>



<a name="165921315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921315">(May 17 2019 at 17:50)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">);</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Reading from x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...keeps y valid.</span>
<span class="w">   </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Writing to x...</span>
<span class="w">   </span><span class="k">if</span><span class="w"> </span><span class="n">some_undecidable_function_always_returning_false</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...kills y. This line is UB.</span>
<span class="w">   </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>you'd have to insert <code>drop(y)</code> after the if, right? but that means there's an error because <code>x</code> gets used while <code>y</code> is still "live"</p>



<a name="165921700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921700">(May 17 2019 at 17:54)</a>:</h4>
<blockquote>
<p>I'm going to be in SF next week for S&amp;P (hearing about Spectre and RIDL and talking about <a href="https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf" target="_blank" title="https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf">https://github.com/chicago-relaxed-memory/spec-eval/blob/master/doc/paper.pdf</a>) so the next step won't be for a bit.</p>
</blockquote>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span>  oh that's interesting. is that related to the "RFUB" (read from untaken branch) example that came up in relaxed memory discussions recently? you mention the hardware attacks but then the directory says sth about relaxed memory^^</p>



<a name="165921798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921798">(May 17 2019 at 17:55)</a>:</h4>
<p>hm, no, seems unrelated. I wonder if there is a connection though...</p>



<a name="165921873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165921873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165921873">(May 17 2019 at 17:56)</a>:</h4>
<p>RFUB is described here: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1217r0.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1217r0.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1217r0.html</a></p>



<a name="165932285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165932285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165932285">(May 17 2019 at 20:07)</a>:</h4>
<p>Hmm, RFUB looks interesting. There's a lot of work on speculative evaluation atm it's very hard to keep up with all of it!</p>



<a name="165932525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165932525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165932525">(May 17 2019 at 20:10)</a>:</h4>
<p>The position that the refcounted borrows model takes (at least at the moment) is that <code>&amp;UnsafeCell&lt;T&gt;</code> is not tracked at all, in the same way that <code>&amp;()</code> isn't tracked.</p>



<a name="165932650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165932650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165932650">(May 17 2019 at 20:12)</a>:</h4>
<p>I am sort of thinking that "As <code>T</code> is to <code>&amp;T</code> so <code>UnsafeCell&lt;T&gt;</code> is to <code>*T</code>.</p>



<a name="165933097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933097">(May 17 2019 at 20:18)</a>:</h4>
<p>Hmm, interesting example, that would indeed be a case that refcounting as it currently stands can't handle.</p>



<a name="165933132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933132">(May 17 2019 at 20:18)</a>:</h4>
<p>I wonder if there's an optimization that refcounting borrows validates that stacked borrows doesn't?</p>



<a name="165933297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933297">(May 17 2019 at 20:21)</a>:</h4>
<p>Something like replacing:</p>
<div class="codehilite"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">v</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>by:</p>
<div class="codehilite"><pre><span></span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</pre></div>



<a name="165933308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933308">(May 17 2019 at 20:21)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="165933651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933651">(May 17 2019 at 20:25)</a>:</h4>
<p>assuming <code>*x: &amp;mut</code>, Stacked Borrows allows this</p>



<a name="165933883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933883">(May 17 2019 at 20:28)</a>:</h4>
<blockquote>
<p>Hmm, RFUB looks interesting. There's a lot of work on speculative evaluation atm it's very hard to keep up with all of it!</p>
</blockquote>
<p>I wonder why that is... ;)</p>



<a name="165933937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165933937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165933937">(May 17 2019 at 20:29)</a>:</h4>
<blockquote>
<p>The position that the refcounted borrows model takes (at least at the moment) is that <code>&amp;UnsafeCell&lt;T&gt;</code> is not tracked at all, in the same way that <code>&amp;()</code> isn't tracked.</p>
</blockquote>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> I see. But doesn't it need tracking insofar as it gets in the way of aliasing <code>&amp;mut</code> being unique? the <code>&amp;UnsafeCell</code> itself allows all sorts of aliasing (same in Stacked Borrows), it doesnt care, but <em>others</em> care that <code>&amp;UnsafeCell</code> is not used to break <em>their</em> properties.</p>



<a name="165935537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935537">(May 17 2019 at 20:49)</a>:</h4>
<blockquote>
<p>assuming <code>*x: &amp;mut</code>, Stacked Borrows allows this</p>
</blockquote>
<p>Er does it? Consider putting it in parallel with</p>
<div class="codehilite"><pre><span></span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="165935616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935616">(May 17 2019 at 20:50)</a>:</h4>
<p>argh hit return rather than shift-return!</p>



<a name="165935795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935795">(May 17 2019 at 20:52)</a>:</h4>
<p>Consider putting it in parallel with:</p>
<div class="codehilite"><pre><span></span><span class="k">unsafe</span><span class="w">  </span><span class="p">{</span><span class="w">  </span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>where <code>p = (x as *mut u8)</code>.</p>



<a name="165935866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935866">(May 17 2019 at 20:53)</a>:</h4>
<p>The first is UB-free, the second has UB, so we can't consider them to be the same as far as optimization is concerned?</p>



<a name="165935936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935936">(May 17 2019 at 20:54)</a>:</h4>
<p>Or are you thinking of a different equivalence relation for "allowed optimization"?</p>



<a name="165935976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165935976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165935976">(May 17 2019 at 20:54)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="165936161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/165936161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#165936161">(May 17 2019 at 20:57)</a>:</h4>
<p>(<span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> there's a checkbox next to the Send button, "Press Enter to send", which you can uncheck and should help with the shift-return annoyance. Also you can edit a message, there's a menu entry in the chevron "message actions" thingy to the right of a message when you hover it, and also a little pencil icon which directly allows editing — I'm assuming here you're not on mobile, that is)</p>



<a name="166021262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166021262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166021262">(May 19 2019 at 12:24)</a>:</h4>
<p>(you can also edit the last message you sent by hitting the "left" arrow key)</p>



<a name="166038873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166038873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166038873">(May 19 2019 at 20:31)</a>:</h4>
<blockquote>
<p>Consider putting it in parallel with:</p>
<div class="codehilite"><pre><span></span><span class="k">unsafe</span><span class="w">  </span><span class="p">{</span><span class="w">  </span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>where <code>p = (x as *mut u8)</code>.</p>
</blockquote>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span>  WDYM "in parallel"? in a different thread?<br>
I assumed <code>x</code> is a mutable ref. Having a mutable ref in scope is sufficient to permit dereferencing it, we even set an attribute in LLVM for that</p>



<a name="166038882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166038882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166038882">(May 19 2019 at 20:31)</a>:</h4>
<p>when <code>x</code> gets in scope, it gets retagged; if another thread can write to taht location we have a data race -&gt; UB</p>



<a name="166716278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716278">(May 28 2019 at 13:26)</a>:</h4>
<p>Back after S&amp;P and Memorial Day...</p>



<a name="166716536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716536">(May 28 2019 at 13:29)</a>:</h4>
<p>Back to the example, which was is rustc allowed to to optimize the safe code (where <code>x: &amp;mut u8</code>):</p>
<div class="codehilite"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="n">v</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>as:</p>
<div class="codehilite"><pre><span></span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="p">}</span>:
</pre></div>


<p>?</p>



<a name="166716601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716601">(May 28 2019 at 13:29)</a>:</h4>
<p>The problem being if there is another thread with access to <code>p = (x as *mut u8)</code> and does:</p>



<a name="166716812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716812">(May 28 2019 at 13:31)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">unsafe</span><span class="w">  </span><span class="p">{</span><span class="w">  </span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="166716953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716953">(May 28 2019 at 13:33)</a>:</h4>
<p>The original has no UB, since if <code>c</code> is true, the safe code writes to <code>x</code> and the unsafe code does nothing, and vice versa if <code>c</code> is false,</p>



<a name="166716993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716993">(May 28 2019 at 13:33)</a>:</h4>
<p>but the "optimized" version has UB since there's a race condition.</p>



<a name="166716998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166716998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166716998">(May 28 2019 at 13:33)</a>:</h4>
<p>it is UB to write to memory that other threads have an <code>&amp;mut</code> to</p>



<a name="166717019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717019">(May 28 2019 at 13:33)</a>:</h4>
<p>that violates the exclusive nature of <code>&amp;mut</code></p>



<a name="166717051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717051">(May 28 2019 at 13:34)</a>:</h4>
<p>this is just the same as having</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">bar</span><span class="p">();</span><span class="w"> </span><span class="c1">// it is UB if bar writes to `x`</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="166717108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717108">(May 28 2019 at 13:34)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> even if the <code>&amp;mut</code> isn't written to?</p>



<a name="166717140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717140">(May 28 2019 at 13:34)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> so first, let's get concurrency out of the way. do you agree that my example is the same?</p>



<a name="166717144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717144">(May 28 2019 at 13:34)</a>:</h4>
<p>all the issues arise sequentially as well</p>



<a name="166717181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717181">(May 28 2019 at 13:35)</a>:</h4>
<p>imagine <code>bar(); if (c) { *x =v; } bar();</code></p>



<a name="166717221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717221">(May 28 2019 at 13:35)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> yes, the example is essentially the same,</p>



<a name="166717302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717302">(May 28 2019 at 13:36)</a>:</h4>
<p>okay. and yes this is UB under current stacked borrows, precisely because we ant to be able to introduce spurious reads and to reorder around unknown function calls.</p>



<a name="166717309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717309">(May 28 2019 at 13:36)</a>:</h4>
<p>the question is whether it's <code>x</code> being in scope that triggers the UB, or an access to <code>x</code> that does.</p>



<a name="166717326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717326">(May 28 2019 at 13:36)</a>:</h4>
<p>like, the goal is to make reordering the <code>if (c) { *x =v; }</code> up or down both legal</p>



<a name="166717336"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717336" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717336">(May 28 2019 at 13:36)</a>:</h4>
<p>(assuming <code>c</code> and <code>v</code>  have no side-effects etc.)</p>



<a name="166717347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717347">(May 28 2019 at 13:36)</a>:</h4>
<p>and for that we need "<code>x</code> is in scope" to be enough for the UB</p>



<a name="166717377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717377">(May 28 2019 at 13:37)</a>:</h4>
<p>and that's why Stacked Borrows currently says it is</p>



<a name="166717385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717385">(May 28 2019 at 13:37)</a>:</h4>
<p>(this is the "retagging" stuff from my earlier blogposts)</p>



<a name="166717411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717411">(May 28 2019 at 13:37)</a>:</h4>
<p>specifically, §3 of <a href="https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html" target="_blank" title="https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html">https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html</a></p>



<a name="166717799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166717799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166717799">(May 28 2019 at 13:40)</a>:</h4>
<p>Do we also have</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">bar</span><span class="p">();</span><span class="w"> </span><span class="c1">// it is UB if bar reads  from`x`</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="166718469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166718469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166718469">(May 28 2019 at 13:47)</a>:</h4>
<p>yes</p>



<a name="166718746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166718746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166718746">(May 28 2019 at 13:50)</a>:</h4>
<p>here's some code for you to play with: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0660fc6baf5827ae3ff94ffa80dec42a" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0660fc6baf5827ae3ff94ffa80dec42a">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0660fc6baf5827ae3ff94ffa80dec42a</a></p>



<a name="166718845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166718845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166718845">(May 28 2019 at 13:50)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so back to your example:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">);</span><span class="w"> </span><span class="c1">// lifetime laundering</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// Reading from x...</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...keeps y valid.</span>
<span class="w">   </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Writing to x...</span>
<span class="w">   </span><span class="k">if</span><span class="w"> </span><span class="n">some_undecidable_function_always_returning_false</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// ...kills y. This line is UB.</span>
<span class="w">   </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>isn't rustc allowed to hoist the read of <code>y</code> in the UB line out of the conditional?</p>



<a name="166718890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166718890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166718890">(May 28 2019 at 13:51)</a>:</h4>
<p>no because it cannot prove that it will happen</p>



<a name="166718936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166718936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166718936">(May 28 2019 at 13:51)</a>:</h4>
<p>and <code>y</code> is not an argument to the function so rust can't know how big its "scope" is</p>



<a name="166719023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719023">(May 28 2019 at 13:52)</a>:</h4>
<p>the optimizations above relied on the fact that these were fn arguments, which are assumed to have a "scope" that's at least the fn call</p>



<a name="166719043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719043">(May 28 2019 at 13:52)</a>:</h4>
<p>(that's the "barriers"/"protectors" from stacked borrows)</p>



<a name="166719044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719044">(May 28 2019 at 13:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> really? We're banning hoisting loads out of conditionals? This seems quite drastic!</p>



<a name="166719081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719081">(May 28 2019 at 13:52)</a>:</h4>
<p>well in C you cannot do that either unless you can prove that the load is okay</p>



<a name="166719225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719225">(May 28 2019 at 13:54)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Can't you? I suspect a lot of C compilers do :(</p>



<a name="166719253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719253">(May 28 2019 at 13:54)</a>:</h4>
<p>though in this specific case C might be able to rule that <code>some_undecidable_function_always_returning_false</code> cannot possibly deallocate <code>y</code> and hence the load is okay. which is interesting. this will come up when figuring out how to formalize LLVM's <code>noalias</code> <em>inside</em> functions</p>



<a name="166719278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719278">(May 28 2019 at 13:54)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> I'd be surprised if for something like</p>
<div class="codehilite"><pre><span></span><span class="kt">int</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">bool</span> <span class="n">b</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="o">*</span><span class="n">i</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></div>


<p>any C compiler would hoist the load</p>



<a name="166719305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719305">(May 28 2019 at 13:55)</a>:</h4>
<p>it is easy to construct miscompilations from that</p>



<a name="166719381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719381">(May 28 2019 at 13:56)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> won't most compilers hoist loads out of a loop?</p>



<a name="166719610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719610">(May 28 2019 at 13:58)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> they may only do that if they can either prove that the loop iterates at least once, or have another argument for why that pointer is okay</p>



<a name="166719661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166719661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166719661">(May 28 2019 at 13:59)</a>:</h4>
<p>this is one of the reasons why I want the aliasing stuff for all arguments even if they dont get used -- so that Rust can do such hoisting without such concerns</p>



<a name="166720203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166720203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166720203">(May 28 2019 at 14:06)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> yes, I was thinking of hoisting out of do-loops rather than while loops.</p>



<a name="166720738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can%20we%20replace%20the%20stack%20in%20%22stacked%20borrows%22%20by%20Drop%3F/near/166720738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Can.20we.20replace.20the.20stack.20in.20.22stacked.20borrows.22.20by.20Drop.3F.html#166720738">(May 28 2019 at 14:12)</a>:</h4>
<p>yes that's a different situation</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>